package com.azxc.rapid.plus.util;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.azxc.rapid.plus.user.entity.WestoneSectors;
import com.azxc.rapid.plus.user.entity.WestoneStaff;
import com.azxc.rapid.plus.user.entity.WestoneVersion;
import lombok.extern.slf4j.Slf4j;
import org.codehaus.jackson.map.ObjectMapper;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author linym
 * @className: WestoneUtil
 * @description:
 * @date 2023/1/16 15:28
 * @version：1.0
 */
@Slf4j
public class WestoneUtil {

	private static String appKey = "APPAD3BADB33B0622F1BE4C0E5A";
	private static String	corpId = "787987360344064";
	private static String  corpSecret = "a1024mo37JlrK3u2_MR7yNv8rKXsGD819_sNsrJZ4Ubg0zSi5y_0k568xPRQ7TIe951";

	//全量同步通讯录
	public static void synchronizationAll(){
		String url = "https://cx-service.westonecloud.com/api/apps/v1/access-tokens?app_key=" + appKey + "&corp_id=" + corpId + "&corp_secret=" + corpSecret;
		String r = HttpUtil.get(url);
		JSONObject jobject = JSONObject.parseObject(r);

		String url2 = "https://cx-service.westonecloud.com/api/apps/v1/address-book?access_token="+jobject.getString("access_token");
		r = HttpUtil.get(url2);
		jobject = JSONObject.parseObject(r);
		System.out.println(jobject);
		WestoneVersion westoneVersion =	JSONObject.toJavaObject(jobject,WestoneVersion.class);
		System.out.println(westoneVersion);
		JSONArray array= jobject.getJSONArray("sectors");
		for(int i=0;i<array.size();i++){
			WestoneSectors westoneSectors =	JSONObject.toJavaObject(array.getJSONObject(i),WestoneSectors.class);
			System.out.println(westoneSectors);
		}
		JSONArray array2= jobject.getJSONArray("staffs");
		for(int i=0;i<array2.size();i++){
			WestoneStaff westoneStaff =	JSONObject.toJavaObject(array2.getJSONObject(i),WestoneStaff.class);
			westoneStaff.setSectorId(array2.getJSONObject(i).getJSONArray("sector_staffs").getJSONObject(0).getString("sector_id"));
			westoneStaff.setShowOrder(array2.getJSONObject(i).getJSONArray("sector_staffs").getJSONObject(0).getString("show_order"));
			System.out.println(westoneStaff);
		}

	}

	//推送消息


	/*public static void main(String[] args) throws IOException {
		String url = "https://cx-service.westonecloud.com/api/apps/v1/access-tokens?app_key=" + appKey + "&corp_id=" + corpId + "&corp_secret=" + corpSecret;
		String r = HttpUtil.get(url);
		JSONObject jobject = JSONObject.parseObject(r);
		System.out.println(jobject);
		String url2 ="https://cx-service.westonecloud.com/api/apps/v1/push?access_token="+jobject.getString("access_token");;
		System.out.println(url2);
		JSONObject body =new JSONObject();
		body.put("type","ACCOUNT");
		List<String> mobileList=new ArrayList<>();
		mobileList.add("782995517303168");
		//mobileList.add("791136265437248");
		body.put("recipients",mobileList);
		//body.put("payload","String");
		body.put("badge_number",111);
		//body.put("message","String");
		JSONObject content =new JSONObject();
		content.put("msgType","text");
		JSONObject msg =new JSONObject();
		msg.put("subject","testSubject");
		msg.put("content","test1234");
		ObjectMapper objectMapper=new ObjectMapper();
		String obj=objectMapper.writeValueAsString(msg);
		System.out.println("obj:"+obj);


		content.put("msgContent",obj);
		body.put("message", objectMapper.writeValueAsString(content));
		System.out.println(body.toJSONString());

		String result=	HttpUtil.post(url2,body.toJSONString());
		System.out.println(result);
	}*/


}
